<?php
/**
 * 政策
 */
namespace app\model;

use think\Model;
use app\model\Chain;
use app\model\ChainModel;
use app\model\CityModel;
use app\model\PolicyTypeModel;

class PolicyModel extends Model
{
	protected $name = 'Policy';
	
	// 设置当前模型的数据库连接
	protected $connection ='db_yunduan';
	/**
	 * 政策列表
	 *
	 */
	public function GetList($field='*',$condition=array(),$page=1,$limit=15){
		
		$obj = new PolicyModel();
		$obj=$obj->alias('a');
		$where="a.status=1 ";
		/***关键字*/
		
		if(isset($condition['keywords'])&&!empty($condition['keywords'])){
			$where.=" and a.policy_name like '%{$condition['keywords']}%' ";
		}
		/***行业*/
		if(isset($condition['industry_id'])&&!empty($condition['industry_id'])){
			$obj=$obj->join('policy_industry b','a.id=b.policy_id','left');
			$where.=" and b.status=1 and b.industry_id={$condition['industry_id']} ";
		}
		
		/***云端产业链*/
		if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
			$obj=$obj->join('policy_chain c','a.id=c.policy_id','left');
			$where.=" and c.status=1 and c.chain_id={$condition['yun_chain_id']} ";
		}
		/***地方产业链*/
		if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
			$ChainModel=new Chain();
			$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
			
			$obj=$obj->join('policy_chain c','a.id=c.policy_id','left');
			$where.=" and c.status=1 and c.chain_id=$chain_id ";
		}else{
			//全部产业链
			if(isset($condition['user_id'])&&!empty($condition['user_id'])){
				$ChainModel=new Chain();
				$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
				if(!empty($chain_ids)){
					$chain_ids=implode(',',$chain_ids);
					$where.=" and c.chain_id in ($chain_ids) ";
				}
			}
		}
		
		/***城市*/
		if(isset($condition['city_id'])&&!empty($condition['city_id'])){
			$where.=" and (a.province_id={$condition['city_id']} or a.city_id={$condition['city_id']} or a.area_id={$condition['city_id']} ) ";
		}
		
		/***类型*/
		if(isset($condition['type_id'])&&!empty($condition['type_id'])){
			$where.=" and FIND_IN_SET('{$condition['type_id']}',a.type_id) ";
		}
		$listobj=$obj->field($field)->where($where)->order('a.list_order','desc')->order('a.id','asc')->paginate($limit)->toArray();
		
		
		foreach ($listobj['data'] as &$val){
			$val['city_name']=$this->GetCityName($val);
			$val['type_name']=$this->GetTypeName($val);
			//$val['chain_name']=$this->GetChainName($val);
		}
		return $listobj;    
	}
	/**
	 * 政策详情
	 *
	 */
	public function detail($id){
		$PolicyModel = PolicyModel::find($id);
		return $PolicyModel;
	}
	 /**
     * 获取园区数量
     */
    public function GetTotal($condition=array()){  	
    	$obj = new PolicyModel();
    	$obj=$obj->alias('a');
    	$where="a.status=1 ";
    	/***关键字*/
    	
    	if(isset($condition['keywords'])&&!empty($condition['keywords'])){
    		$where.=" and a.policy_name like '%{$condition['keywords']}%' ";
    	}
    	/***行业*/
    	if(isset($condition['industry_id'])&&!empty($condition['industry_id'])){
    		$obj=$obj->join('policy_industry b','a.id=b.policy_id','left');
    		$where.=" and b.status=1 and b.industry_id={$condition['industry_id']} ";
    	}
    	
    	/***云端产业链*/
    	if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
    		$obj=$obj->join('policy_chain c','a.id=c.policy_id','left');
    		$where.=" and c.status=1 and c.chain_id={$condition['yun_chain_id']} ";
    	}
    	/***地方产业链*/
    	if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
    		$ChainModel=new Chain();
    		$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
    		
    		$obj=$obj->join('policy_chain c','a.id=c.policy_id','left');
    		$where.=" and c.status=1 and c.chain_id=$chain_id ";
    	}else{
    		//全部产业链
    		if(isset($condition['user_id'])&&!empty($condition['user_id'])){
    			$ChainModel=new Chain();
    			$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
    			if(!empty($chain_ids)){
    				$chain_ids=implode(',',$chain_ids);
    				$where.=" and c.chain_id in ($chain_ids) ";
    			}
    		}
    	}
    	
    	/***城市*/
    	if(isset($condition['city_id'])&&!empty($condition['city_id'])){
    		$where.=" and (a.province_id={$condition['city_id']} or a.city_id={$condition['city_id']} or a.area_id={$condition['city_id']} ) ";
    	}
    	
    	$total=$obj->where($where)->count();
    	
    	return $total;
    }
    /**
     * 获得城市
     */
    public function GetCityName($info){
    	$CityModel=new CityModel();
    	$param=[];
    	if(isset($info['province_id'])){
    		$param[]=$info['province_id'];
    	}
    	if(isset($info['city_id'])){
    		$param[]=$info['city_id'];
    	}
    	if(isset($info['area_id'])){
    		$param[]=$info['area_id'];
    	}
    	
    	$city=$CityModel->GetCityNameByCityIds($param);
    	return empty($city)?'':implode(',',$city);
    	
    }
    /**
     * 获得类型
     */
    public function GetTypeName($info){
    	$PolicyTypeModel=new PolicyTypeModel();
    	$PolicyType=$PolicyTypeModel->GetListColumn();
    	$typeid=explode(',',$info['type_id']);
    	$type_name=[];
    	foreach ($typeid as $val1){
    		if(isset($PolicyType[$val1])){
    			$type_name[]=$PolicyType[$val1];
    		}
    	}
    	return implode(',',$type_name);
    }
    /**
     * 获得产业
     */
    public function GetChainName($info){
    	$ChainModel=new ChainModel();
    	
    	$res=$ChainModel->GetChainNameByPolicy($info['id']);
    	return implode(',',$res);
    }

}